package fr.labri.macchiato.ui.preferences;

import java.util.ArrayList;
import java.util.List;

import com.smartgwt.client.util.EventHandler;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.DropOverEvent;
import com.smartgwt.client.widgets.events.DropOverHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import fr.labri.macchiato.core.api.Finder;
import fr.labri.macchiato.core.model.UserPreferences;
import fr.labri.macchiato.core.service.Services;

/**
 * A widget that allows the user to choose his preferred {@link BusinessEntity}
 * If the user has chosen to save permanently his preferences, so the logos that
 * have been dropped into the selectedBusinessEntities layout, should remain
 * after close.
 * 
 * @author Michelle AVOMO
 * 
 * 
 */

public class FeatureWidget extends VLayout implements PreferencesWidget {

	private static final String BORDER_COLOR = "#008A77";
	private static final String SELECTED_SHOPS = "<i> Modules favoris</i>";
	private static final String LABEL = "<b>Déplacez les images pour sélectionner vos modules préférés</b>";
	private static final String WIDGET_TITLE = "Mes modules préférés";
	private HLayout selectedFeatures;
	private HLayout availableFeatures;

	private List<Finder> favoriteFeatures;

	/**
	 * Constructor
	 */
	public FeatureWidget() {
		favoriteFeatures = new ArrayList<Finder>();
		setWidth100();

		Label textLabel = new Label(LABEL);
		textLabel.setWidth(600);

		addMember(textLabel);

		createListFinderContainer();
		createPreferedFindersContainer();
		eventsAfterDropping();

		// Adding the components to the HLAyout
		addMember(availableFeatures);
		addMember(selectedFeatures);

	}

	/******************************* PRIVATE METHODS ************************************************/

	/**
	 * What should occur after the user dropped a given logo into the
	 * selectedBusinessEntities container.
	 */
	private void eventsAfterDropping() {
		selectedFeatures.addDropOverHandler(new DropOverHandler() {
			public void onDropOver(DropOverEvent event) {
				selectedFeatures.setBorder(BORDER_COLOR);
				getPreferedFinders();
			}

		});

		availableFeatures.addDropOverHandler(new DropOverHandler() {

			public void onDropOver(DropOverEvent event) {
				favoriteFeatures.remove(getDraggedFinder());

			}
		});
	}

	/**
	 * Creates the container for the dynamic container with the prefered objects
	 */
	private void createPreferedFindersContainer() {
		// Selected providers
		selectedFeatures = new HLayout(10);
		selectedFeatures.setContents(SELECTED_SHOPS);

		// Properties for the selected providers
		selectedFeatures.setLayoutMargin(20);
		selectedFeatures.setLeft(120);
		selectedFeatures.setTop(10);
		selectedFeatures.setShowEdges(false);
		selectedFeatures.setCanAcceptDrop(true);
		selectedFeatures.setAnimateMembers(true);
		selectedFeatures.setShowDragPlaceHolder(true);
		selectedFeatures.setDropLineThickness(5);
		selectedFeatures.setBorder("3px solid #008A77");

		selectedFeatures
				.setTitle("Vos modules favoris. Gissez-deposez pour retirer de la liste");

	}

	/**
	 * Creates the container with the list of logos dynamic charged from the
	 * database
	 */

	private void createListFinderContainer() {

		// List of the providers
		availableFeatures = new HLayout(10);
		availableFeatures.setContents("<i>Liste des modules </i>");

		// properties for the list of the selected providers
		availableFeatures.setLayoutMargin(20);
		availableFeatures.setCanAcceptDrop(true);
		availableFeatures.setAnimateMembers(true);
		availableFeatures.setDropLineThickness(5);
		availableFeatures.setBorder("3px solid #adcf4f");

	}

	/**
	 * Retrieves logos from the database and adds them into the container
	 */
	private void addLogosToList() {
		for (Finder be : Services.getInstance().getProductFinders()) {
			if (!favoriteFeatures.contains(be)) {
				FinderLogo logo = new FinderLogo(be);
				availableFeatures.addMember(logo);
			}
		}
	}

	/**
	 * Retrieves the id of the favorite shops
	 */
	private List<Finder> getPreferedFinders() {

		Finder bu = getDraggedFinder();

		if (!favoriteFeatures.contains(bu))
			favoriteFeatures.add(bu);

		return favoriteFeatures;
	}

	/******************************* METHODS FROM THE INTERFACE ************************************************/

	public void addToParent(Window preference) {
		preference.addItem(this);

	}

	public void collectUserPreferences(UserPreferences preference) {
		preference.setFavoriteFinders(favoriteFeatures);
	}

	public Boolean isValid() {
		return true;
	}

	public void restoreUserPreferences(UserPreferences preferences) {
		availableFeatures.removeMembers(availableFeatures
				.getMembers());
		selectedFeatures.removeMembers(selectedFeatures
				.getMembers());

		if (preferences.getFavoriteFinders() != null
				&& !preferences.getFavoriteFinders().isEmpty())
			setFavoriteFinders(preferences.getFavoriteFinders());
		addLogosToList();

	}

	/***************************************************** PRIVATE METHODS **************************************************************/

	private void setFavoriteFinders(
			List<Finder> favoriteFinders) {
		this.favoriteFeatures = favoriteFinders;
		for (Finder bu : favoriteFinders) {
			FinderLogo logo = new FinderLogo(bu);
			selectedFeatures.addMember(logo);
		}
	}

	private Finder getDraggedFinder() {
		FinderLogo ltd = (FinderLogo) EventHandler
				.getDragTarget();

		Finder bu = ltd.getFinder();

		return bu;
	}

	@Override
	public String getTitle() {
		return WIDGET_TITLE;
	}
}
